using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    private static UIManager _instance;

    public static UIManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindAnyObjectByType<UIManager>();
            }

            return _instance;
        }
    }

    public TextMeshProUGUI textScore;
    public TextMeshProUGUI playerHp;
    public TextMeshProUGUI textBoomCount;
    public Button buttonPause;
    public Button buttonResume;


    // 持有gameover实例以及text等对象实例
    public GameObject gameOverUI;
    public TextMeshProUGUI textBestScore;
    public TextMeshProUGUI textCurrentScore;
    public Button buttonRestart;
    public Button buttonQuit;

    void Start()
    {
        buttonPause.onClick.AddListener(OnButtonPausePressed);
        buttonResume.onClick.AddListener(OnButtonResumePressed);
        buttonRestart.onClick.AddListener(OnButtonRestartPressed);
        buttonQuit.onClick.AddListener(OnButtonQuitePressed);
    }

    // Update is called once per frame
    void Update()
    {
    }

    public void TextScoreUpdate(int score)
    {
        textScore.text = score + "";
    }

    public void TextPlayerHpUpdate(int hp)
    {
        playerHp.text = hp + "";
    }

    public void BoomCountUpdate(int boomCount)
    {
        textBoomCount.text = boomCount + "";
    }

    void OnButtonPausePressed()
    {
        buttonPause.gameObject.SetActive(false);
        buttonResume.gameObject.SetActive(true);
        GameManager.Instance.PauseGame();
        AudioManager.Instance.PlayButtonAudio();
    }

    void OnButtonResumePressed()
    {
        buttonPause.gameObject.SetActive(true);
        buttonResume.gameObject.SetActive(false);
        GameManager.Instance.ResumeGame();
        AudioManager.Instance.PlayButtonAudio();
    }

    void OnButtonRestartPressed()
    {
        AudioManager.Instance.PlayButtonAudio();
        // 禁用按钮，防止重复点击
        buttonRestart.interactable = false;
        // 延迟调用场景切换方法
        LoadCurrentScene();
        // Invoke("LoadCurrentScene", AudioManager.Instance.buttonClinkAudio.clip.length);
    }

    void LoadCurrentScene()
    {
        GameManager.Instance.RestartGame();
    }

    void OnButtonQuitePressed()
    {
        AudioManager.Instance.PlayButtonAudio();
        GameManager.Instance.QuitGame();
    }

    public void ShowGameOverUI()
    {
        textBestScore.text = PlayerPrefs.GetInt("BestScore", 0) + "";
        textCurrentScore.text = GameManager.Instance.score + "";
        gameOverUI.SetActive(true);
        //更新最高分
        if (PlayerPrefs.GetInt("BestScore", 0) < GameManager.Instance.score)
        {
            PlayerPrefs.SetInt("BestScore", GameManager.Instance.score);
        }
    }
}